package com.adtech.adms.commons.util;

import java.util.Map;
import java.util.Stack;
import java.util.concurrent.ConcurrentHashMap;

import com.adtech.adms.commons.tree.TreeNode;

/**
 * 
 * tree工具
 * @author 李源 
 * @since 2011-8-11 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class TreeUtil {
	
	private static final Map<String,String> companyTreeMap = new ConcurrentHashMap<String,String>();
	private static final Map<String,String> regionTreeMap = new ConcurrentHashMap<String,String>();
	
	public interface OnGetNodeListener<T> {
		public boolean onGetNode(TreeNode<T> node);
	};

	public static <T> TreeNode<T> iteratorNodes(TreeNode<T> node, OnGetNodeListener<T> listener) {
		Stack<TreeNode<T>> nodes = new Stack<TreeNode<T>>();

		nodes.push(node);
		while (!nodes.isEmpty()) {
			TreeNode<T> bufNode = nodes.pop();
			if (listener.onGetNode(bufNode)) {
				return bufNode;
			} else {
				if (!bufNode.isLeaf()) {
					nodes.addAll(bufNode.getNodes());
				}
			}
		}
		return null;
	}
//
//	public static void setTreeNodePath(Stack nodes, TreeNode node) {
//		nodes.push(node);
//		if (node.getParent() != null) {
//			setTreeNodePath(nodes, node.getParent());
//		}
//	}
	
	public static void putCompanyTree(String key,String value) {
		companyTreeMap.put(key, value);
	}
	
	public static void putRegionTree(String key,String value) {
		regionTreeMap.put(key, value);
	}
	
	public static String getCompanyTree(String key) {
		return companyTreeMap.get(key);
	}
	
	public static String getRegionTree(String key) {
		return regionTreeMap.get(key);
	}

    public static void clearCompanyTree() {
        companyTreeMap.clear();
    }

    public static void clearRegionTree() {
        regionTreeMap.clear();
    }
}
